docs: clarify iterable render returns - #8606
Conversation
There was a problem hiding this comment.
Pull request overview
This PR updates the React class component reference docs to clarify that render may return iterables (not just arrays) of React nodes, and to warn about pitfalls when returning iterators that can be consumed across renders (notably in development).
Changes:
- Expands the “Returns” description to include iterables of React nodes.
- Adds a caveat warning about returning iterators from
renderand the need to create a fresh iterator per render.
Suppressed comments (1)
src/content/reference/react/Component.md:586
- This new iterator caveat largely repeats the Strict Mode bullet immediately below and implies extra development-only renders in general. Consider reordering so the Strict Mode explanation comes first, and tighten the iterator warning to the core issue (iterators are consumed).
- If you return an iterator from `render`, make sure each render creates a fresh iterator. React may render more than once in development, and a reused iterator may already be consumed. When possible, prefer returning an array or another reusable iterable.
- When [Strict Mode](/reference/react/StrictMode) is on, React will call `render` twice in development and then throw away one of the results. This helps you notice the accidental side effects that need to be moved out of the `render` method.
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| #### Returns {/*render-returns*/} | ||
|
|
||
| `render` can return any valid React node. This includes React elements such as `<div />`, strings, numbers, [portals](/reference/react-dom/createPortal), empty nodes (`null`, `undefined`, `true`, and `false`), and arrays of React nodes. | ||
| `render` can return any valid React node. This includes React elements such as `<div />`, strings, numbers, [portals](/reference/react-dom/createPortal), empty nodes (`null`, `undefined`, `true`, and `false`), arrays of React nodes, and iterables of React nodes. |
Size changesDetails📦 Next.js Bundle Analysis for react-devThis analysis was generated by the Next.js Bundle Analysis action. 🤖 This PR introduced no changes to the JavaScript bundle! 🙌 |
|
Thanks for the review. Reordered the caveats so the Strict Mode explanation comes first, and tightened the iterator note to the consumption issue instead of repeating the extra development render. |
Summary
Clarify that
rendermay return iterables of React nodes, and warn that iterators should be recreated each render.Fixes #286
Why
This avoids confusion around returning iterators from class components in development, where React may call
rendermore than once and a reused iterator can already be consumed.Changes